﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using AKJ.Sharepoint.Branding.Config;
using System.Web.UI;

namespace AKJ.Sharepoint.Branding.Code
{
    public class BrandingManager_old
    {        
        /// <summary>
        /// Clear the cache, and insure we have a new fresh object
        /// </summary>
        public static void ClearBrandingCache()
        {
            HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Master.ToString()] = new Dictionary<string, BrandingPair>();
            HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Css.ToString()] = new Dictionary<string, BrandingPair>();
            HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Cache.ToString()] = new List<string>();
        }

        public static void MarkPathAsInitialized(string relPath)
        {
            relPath = relPath.ToLower();

            if (HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Cache.ToString()] == null)
            {
                HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Cache.ToString()] = new List<string>();
            }

            List<string> cachedPaths = HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Cache.ToString()] as List<string>;

            if (cachedPaths.Contains(relPath) == false)
            {
                cachedPaths.Add(relPath);                
            }
            else
            {
                cachedPaths[cachedPaths.LastIndexOf(relPath)] = relPath;
            }
        }

        /// <summary>
        /// ....
        /// </summary>
        /// <param name="type"></param>
        /// <param name="priority"></param>
        /// <param name="value"></param>
        public static void InsertBrandingElement(BrandingElementType type, string relPath, int priority, string value)
        {
            string key = type.ToString();
            
            if (string.IsNullOrEmpty(value)) return;                       

            if (HttpContext.Current.Application[key] == null)          
                HttpContext.Current.Application[key] = new Dictionary<string, BrandingPair>();
            
            Dictionary<string, BrandingPair> pairs = HttpContext.Current.Application[key] as Dictionary<string, BrandingPair>;
                        
            // If no entry exist or the new one is larger use the new one
            if (pairs.ContainsKey(relPath) == false )
            {
                pairs.Add(relPath,new BrandingPair(priority, value));                
            }
            else if(priority > pairs[relPath].Priority) // If priority of new item is higher then replace
            {
                pairs[relPath] = new BrandingPair(priority, value);                
            }
        }

        public static bool IsPathBranded(string path)
        {
            path = path.ToLower();
            List<string> cachedPath = HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Cache.ToString()] as List<string>;

            return (cachedPath != null && cachedPath.Contains(path));            
        }

        /// <summary>
        /// Returns the current Branding Element of the requested type (can be null).
        /// </summary>
        /// <param name="type"></param>
        /// <returns>Check for null</returns>
        public static BrandingPair GetCurrentBrandingElement(BrandingElementType type, string relPath)
        {
            Dictionary<string, BrandingPair> pairs = HttpContext.Current.Application[type.ToString()] as Dictionary<string, BrandingPair>;

            if (pairs != null && pairs.ContainsKey(relPath))
                return pairs[relPath];
            else
                return null;
        }
    }
}
